//===----------------------------------------------------------------------===//
//
//                         CMU-DB Project (15-445/645)
//                         ***DO NO SHARE PUBLICLY***
//
// Identification: src/page/b_plus_tree_page.cpp
//
// Copyright (c) 2018, Carnegie Mellon University Database Group
//
//===----------------------------------------------------------------------===//

#include "storage/page/b_plus_tree_page.h"

namespace bustub {

/*
 * Helper methods to get/set page type
 * Page type enum class is defined in b_plus_tree_page.h
 */
auto BPlusTreePage::IsLeafPage() const -> bool { 
    if(this->page_type_==IndexPageType::LEAF_PAGE)
        return true;
    return false; 
}
auto BPlusTreePage::IsRootPage() const -> bool { 
    if(this->parent_page_id_!=this->page_id_) //如果当前页面的parentid等于当前页面id，就是root，否则不是
        return false;
    return true;
}
void BPlusTreePage::SetPageType(IndexPageType page_type) {
    this->page_type_=page_type;
}
IndexPageType BPlusTreePage::GetPageType(){
    return this->page_type_;
}

/*
 * Helper methods to get/set size (number of key/value pairs stored in that
 * page)
 */
auto BPlusTreePage::GetSize() const -> int { 
    return this->size_;
}
void BPlusTreePage::SetSize(int size) {
    this->size_=size;
}
void BPlusTreePage::IncreaseSize(int amount) {
    this->size_ = this->size_+amount;
}

/*
 * Helper methods to get/set max size (capacity) of the page
 */
auto BPlusTreePage::GetMaxSize() const -> int { 
    return this->max_size_;
}
void BPlusTreePage::SetMaxSize(int size) {
    this->max_size_=size;
}

/*
 * Helper method to get min page size
 * Generally, min page size == max page size / 2
 */
auto BPlusTreePage::GetMinSize() const -> int {
    // 需要根据叶子和非叶子节点进行两种不同的计算，叶子节点要大于等于n/2 向下取整，非叶子节点要大于n/2向上取整
    if(this->IsLeafPage())
        return this->max_size_/2;
    return (this->max_size_+1)/2;
}

/*
 * Helper methods to get/set parent page id
 */
auto BPlusTreePage::GetParentPageId() const -> page_id_t { 
    return this->parent_page_id_;
}
void BPlusTreePage::SetParentPageId(page_id_t parent_page_id) {
    this->parent_page_id_ = parent_page_id;
}

/*
 * Helper methods to get/set self page id
 */
auto BPlusTreePage::GetPageId() const -> page_id_t { 
    return this->page_id_;
}
void BPlusTreePage::SetPageId(page_id_t page_id) {
    this->page_id_=page_id;
}

/*
 * Helper methods to set lsn
 */
void BPlusTreePage::SetLSN(lsn_t lsn) { lsn_ = lsn; }

}  // namespace bustub
